using System;
using Gtk;
using MySql.Data.MySqlClient;

public partial class MainWindow : Gtk.Window
{
	private MySqlConnection connection;
		
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
		
	protected virtual void Conectar (object sender, System.EventArgs e)
	{
		status.Text = "Conectado";
		connection = new MySqlConnection("Server=Localhost;Database=dbalejandro;Uid=root;Pwd=sistemas");	
		connection.Open();
		
	}
	
	protected virtual void consultaLibro (object sender, System.EventArgs e){
		MySqlCommand command = connection.CreateCommand();
		command.CommandText = "select * from libro";
		
		MySqlDataReader dataReader = command.ExecuteReader();
				
		TreeViewColumn[] columns = treeview.Columns;
		foreach(TreeViewColumn column in columns)
			treeview.RemoveColumn(column);
		
		
		for(int index = 0; index < dataReader.FieldCount; index++){
			treeview.AppendColumn(dataReader.GetName(index),new CellRendererText(),"text", index);
		}
		
		int count = dataReader.FieldCount;
		Type[] types = new Type[count];
		for(int i = 0; i < count;i++){
			types[i] = typeof(string);	
		}
		
	 	ListStore libroListStore = new ListStore(types);
		
		while(dataReader.Read()){	
			string[] row = new string[count];
			for(int i = 0; i < count; i++){
				row[i] = dataReader[i].ToString();
			}
			libroListStore.AppendValues(row);
		}	
		
		treeview.Model = libroListStore;

		treeview.ShowAll();
		dataReader.Close();
	}
	
	protected virtual void sendConsult (object sender, System.EventArgs e){
		string consulta = entry.Text;

		//Console.WriteLine(consulta);
		MySqlCommand command = connection.CreateCommand();
		command.CommandText = consulta;
		
		MySqlDataReader dataReader = command.ExecuteReader();
		
		TreeViewColumn[] columns = treeview.Columns;
		foreach(TreeViewColumn column in columns)
			treeview.RemoveColumn(column);
		
		
		for(int index = 0; index < dataReader.FieldCount; index++){
			treeview.AppendColumn(dataReader.GetName(index),new CellRendererText(),"text", index);
		}
		
		int count = dataReader.FieldCount;
		Type[] types = new Type[count];
		for(int i = 0; i < count;i++){
			types[i] = typeof(string);	
		}
		
	 	ListStore libroListStore = new ListStore(types);
		
		while(dataReader.Read()){	
			string[] row = new string[count];
			for(int i = 0; i < count; i++){
				row[i] = dataReader[i].ToString();
			}
			libroListStore.AppendValues(row);
		}	
		
		treeview.Model = libroListStore;
		
		treeview.ShowAll();
		dataReader.Close();
	}
	
	protected virtual void consultaAutor (object sender, System.EventArgs e){
		
		MySqlCommand command = connection.CreateCommand();
		command.CommandText = "select * from autor";
		
		MySqlDataReader dataReader = command.ExecuteReader();
		
		TreeViewColumn[] columns = treeview.Columns;
		foreach(TreeViewColumn column in columns)
			treeview.RemoveColumn(column);
				
		for(int index = 0; index < dataReader.FieldCount; index++){
			treeview.AppendColumn(dataReader.GetName(index),new CellRendererText(),"text", index);
		}
		
		int count = dataReader.FieldCount;
		Type[] types = new Type[count];
		for(int i = 0; i < count;i++){
			types[i] = typeof(string);	
		}
		
	 	ListStore autorListStore = new ListStore(types);
		
		while(dataReader.Read()){	
			string[] row = new string[count];
			for(int i = 0; i < count; i++){
				row[i] = dataReader[i].ToString();
			}
			autorListStore.AppendValues(row);
		}	
		
		treeview.Model = autorListStore;
		treeview.ShowAll();
		dataReader.Close();
	}
	
	protected virtual void Desconectar (object sender, System.EventArgs e){
		connection.Close();
		status.Text = "Desconectado";
	}
	
	protected virtual void vaciarTreeView (object sender, System.EventArgs e)
	{
		TreeViewColumn[] columns = treeview.Columns;
		foreach(TreeViewColumn column in columns)
			treeview.RemoveColumn(column);
	}
	
	
}

